<script>
import { mapMutations, mapGetters } from "vuex"
import storage from "@/uni-sub-base/utils/storage.js"

import { GetAppSetting } from "@/service/api/web.js"
import config from "@/config/config.js"

import { getLocation } from "@/uni-sub-base/utils/mapUtil.js"

export default {
    data() {
        return {
            homeTab: null,
        }
    },
    onLaunch: function (e) {
        console.log("App Launch")
        // return
        //#ifdef APP-PLUS
        switch (uni.getSystemInfoSync().platform) {
            case "android":
                //console.log('运行Android上')
                plus.navigator.closeSplashscreen()
                break
            case "ios":
                //console.log('运行iOS上')
                plus.navigator.closeSplashscreen()
                break
            default:
                //console.log('运行在开发者工具上')
                break
        }
        // #endif

        if (e && e.query && e.query.tab) {
            this.homeTab = e.query.tab
        }
        uni.showLoading({
            title: "加载中",
        })

        //#ifdef APP-PLUS
        if (uni.getSystemInfoSync().platform == "ios") {
            this.getSetting()
        } else {
            this.normalFn()
        }
        //#endif
        //#ifndef APP-PLUS
        this.normalFn()
        //#endif
    },
    onShow: function () {
        console.log("App Show")
    },
    onHide: function () {
        console.log("App Hide")
    },
    methods: {
        ...mapMutations(["SET_AUTH", "SET_SHOW_PRO_IMG"]),
        //已登录的初始化接口
        appLoginedInit() {
            let isShowProImg = storage.getCache("isShowProImg")
            let loginType = storage.getCache("loginType")
            if (isShowProImg != null && String(isShowProImg) != "") {
                this.SET_SHOW_PRO_IMG(isShowProImg)
            }
            //判断当前登录的是哪个平台，初始化进到对应的页面
            let path = ""
            if (loginType == "admin") {
                path = "/pages/index/index"
                let tab = this.homeTab
                if (tab) {
                    let homeBottomTabBar = this.getAppSet().homeBottomTabBar
                    let hasInTab = homeBottomTabBar.find((e) => {
                        return e.name == tab && e.isShow
                    })
                    if (hasInTab) {
                        path = path + "?tab=" + tab
                    }
                }
            }
            if (loginType == "supply") {
                path = "/pages_supplier/packet/index/index"
            }
            uni.reLaunch({
                url: path,
            })
        },
        getSetting() {
            let path = "?ver=" + config.version + "&sysType=2" //1商城 2管理端
            GetAppSetting(path).then((res) => {
                uni.hideLoading()
                if (res.State == 1) {
                    if (res.Data.SyetemType != 0) {
                        //跳转用户端
                        storage.setCache("DefaultSite", res.Data.DefaultSite) //用户端网址
                        this.easyGoTo("/pages/web/index")
                    } else {
                        this.normalFn()
                    }
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        normalFn() {
            getLocation(
                (res) => { },
                (fail) => { },
            )

            //判断登录状态
            let token = storage.getCache("token")
            console.log(token)
            if (token && token != "") {
                //已登录
                this.SET_AUTH(token)
                //已登录初始化需要获取的接口
                this.appLoginedInit()
                //TODO 这里需要待所有初始化都完成再执行
                uni.hideLoading()
            } else {
                //未登录
                uni.hideLoading()
                uni.reLaunch({
                    url: "/pages/login/index",
                })
            }
        },
    },
}
</script>

<style lang="scss">
@import "@/style/common.scss";
@import "@/style/btn.scss";
@import "@/style/tag.scss";
@import "@/style/icon.scss";
@import "@/style/pop.scss";
@import "@/style/drawer.scss";
@import "@/style/work.scss";
@import "@/style/iconfont/iconfont.css";

// @import "@/uni_modules/uview-ui/index.scss";

.uni-calendar {
    position: relative;
    z-index: 99999;
}

.popupright .uni-popup__wrapper {
    width: 750rpx;
}
</style>
